package com.lexin.action.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lexin.action.dao.SequenceDaoImpl;
import com.lexin.action.dao.UserAccountsDaoImpl;
import com.lexin.action.model.Sequence;
import com.lexin.action.model.UserAccounts;
import com.lexin.common.base.TabelName;
import com.lexin.common.utils.DateUtil;


@Service
public class SignupService {
	
	@Autowired
	SequenceDaoImpl sequenceDaoImpl;
	@Autowired
	UserAccountsDaoImpl userAccountsDaoImpl;
	
	/***
	 * 注册
	 * @param userAccounts
	 * @return
	 * @throws Exception
	 */
	public int signup(UserAccounts userAccounts) throws Exception {
		long id = sequenceDaoImpl.select(TabelName.UserAccounts);
		Sequence seq = new Sequence();
		seq.setTable_name(TabelName.UserAccounts);
		seq.setDesc("帐号信息");
		if (id<0) {
			seq.setValue(1);
			if( sequenceDaoImpl.insert(seq)<=0 ) {
				return -1;
			}
			id = 1;
		} else {
			id += 1;
			seq.setValue(id);
			if (sequenceDaoImpl.update(seq)<0) {
				return -1;
			}
		}
		userAccounts.setId(id);
		userAccounts.setCreate_date(DateUtil.now());
		if ( userAccountsDaoImpl.insert(userAccounts)<=0 ) {
			return 0;
		}
		return 1;
	}
	
}
